<template>
  <div class="container" v-if="dataAuth">
    <page-header
      :title="$t('m.Data_statistics')"
      :hasReturn="false"
    ></page-header>

    <div>
      <div style="width:1000px;">
        <div class="data_div">
          <div class="data_div_div">
            <div
              class="gray_btn"
              v-for="(item, index) in optionsTab"
              :key="index"
              :class="{ active1: item.checked }"
              @click="switchTab(item)"
            >
              {{ item.label }}
            </div>
          </div>

          <div v-if="selectTab == 7" class=" data_div_div date_picker">
            <el-date-picker
              v-model="params.rangeTime"
              :picker-options="datePickerOptions"
              :default-value="timeDefaultShow"
              :start-placeholder="$t('m.start_date')"
              :end-placeholder="$t('m.end_date')"
              @change="changeRangeTime"
              type="daterange"
              value-format="timestamp"
              range-separator="~"
              :default-time="['00:00:00', '23:59:59']"
            >
            </el-date-picker>
          </div>
        </div>

        <div class="statistics_div">
          <!-- 租借订单 -->
          <div class="statistics_card">
            <div class="statistics_card_top">
              <div class="statistics_card_title">
                {{ $t('m.Rental_order') }}
              </div>
            </div>
            <div class="statistics_card_div">
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.All_Orders') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.order.totalCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Order_Status_Renting') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.order.rentingCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Order_Status_Unpaid') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.order.unpaidCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Order_Status_Surveying') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.order.surveyingCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Order_Status_Completed') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.order.completedCount }}
                </div>
              </div>
            </div>
          </div>

          <!-- 代理商 -->
          <div class="statistics_card" v-if="agentLevel != 2">
            <div class="statistics_card_top">
              <div class="statistics_card_title">
                {{ $t('m.Partner_Franchisee') }}
              </div>
            </div>
            <div class="statistics_card_div">
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Total_franchisees') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.agent.totalCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.franchiseeProfit') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.agent.revenueAmount | amountFormat }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Franchisee_withdrawal') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.agent.withdrawalAmount | amountFormat }}
                </div>
              </div>
            </div>
          </div>

          <!-- 机柜 -->
          <div class="statistics_card">
            <div class="statistics_card_top">
              <div class="statistics_card_title">
                {{ $t('m.Stations') }}
              </div>
            </div>
            <div class="statistics_card_div statistics_card_line3">
              <div class="statistics_card_div_top statistics_card_content ">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Total_number_stations') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.station.total }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Number_active_stations') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.station.activatedCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Number_be_activated') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.station.toBeActivated }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.activeOnlineStation') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.station.activatedOnlineCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.activeOfflineStation') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.station.activatedOfflineCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.activeLockStation') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.station.activatedLockCount }}
                </div>
              </div>
            </div>
          </div>
          <div class="statistics_card" v-if="userType == 1">
            <div class="statistics_card_top">
              <div class="statistics_card_title">{{ $t('m.Users2') }}</div>
            </div>
            <div class="statistics_card_div">
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Total_users') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.user.userCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Total_users_rent') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.user.rentalCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Bind_card_user') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.user.cardCount }}
                </div>
              </div>
              <!-- <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Users_paid_deposit') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.user.depositCount | numberFormat }}
                </div>
              </div> -->
              <!-- <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Users_without_payment2') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line1">
                  {{ total.user.otherCount }}
                </div>
              </div> -->
            </div>
          </div>
          <div class="statistics_card">
            <div class="statistics_card_top">
              <div class="statistics_card_title">
                {{ $t('m.Merchant_') }}
              </div>
            </div>
            <div class="statistics_card_div statistics_card_line3">
              <div class="statistics_card_div_top statistics_card_content ">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Total_merchants') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.shop.totalCount }}
                </div>
              </div>
              <div
                class="statistics_card_div_top statistics_card_content"
                v-if="userType == 1"
              >
                <div class="statistics_card_top_title1">
                  {{ $t('m.Affiliated_to_platform') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.shop.platformTotalCount }}
                </div>
              </div>
              <div
                class="statistics_card_div_top statistics_card_content"
                v-if="userType == 1"
              >
                <div class="statistics_card_top_title1">
                  {{ $t('m.Affiliated_to_franchisee') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.shop.agentTotalCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.merchantProfit') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.shop.revenueAmount | amountFormat }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Merchant_withdrawal') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.shop.withdrawalAmount | amountFormat }}
                </div>
              </div>
            </div>
          </div>

          <div class="statistics_card">
            <div class="statistics_card_top">
              <div class="statistics_card_title">{{ $t('m.Power_bank') }}</div>
            </div>
            <div class="statistics_card_div statistics_card_line3">
              <div class="statistics_card_div_top statistics_card_content ">
                <div class="statistics_card_top_title1">
                  {{ $t('m.totalNumberOfChargingTreasure') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.powerBank.total }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Station_Exceptional') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.powerBank.abnormalCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Station_untapped') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.powerBank.unused }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Order_Status_Renting') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.powerBank.lease }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Station_Sold') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.powerBank.soldCount }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Station_Damaged') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.powerBank.broken }}
                </div>
              </div>
              <div class="statistics_card_div_top statistics_card_content">
                <div class="statistics_card_top_title1">
                  {{ $t('m.Inventory') }}
                </div>
                <div class="statistics_card_top_title2 statistics_card_line2">
                  {{ total.powerBank.stockCount }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      datePickerOptions: {
        disabledDate (time) {
          // return time.getTime() > Date.now() - 8.64e6
          let current_time = new Date().Format('yyyy-MM-dd') + ' 23:59:59' //时间日期为：‘当前日期 23:59:59’
          let t = new Date(current_time).getTime() //‘当前日期 23:59:59’的时间戳
          return time.getTime() > t
        }
      },
      selectTab: '',
      optionsTab: [
        {
          checked: true,
          value: 1,
          label: this.$t('m.Today'),
          rangeTime: this.$util.getAroundTodayRangeTime()
        },
        {
          checked: false,
          value: 2,
          label: this.$t('m.Yesterday'),
          rangeTime: this.$util.getAroundYesterdayRangeTime()
        },
        {
          checked: false,
          value: 3,
          label: this.$t('m.Last_7days'),
          rangeTime: this.$util.getAround7DayRangeTime()
        },
        {
          checked: false,
          value: 4,
          label: this.$t('m.Last_30days'),
          rangeTime: this.$util.getAround30DayRangeTime()
        },
        {
          checked: false,
          value: 5,
          label: this.$t('m.Last_90days'),
          rangeTime: this.$util.getAround90DayRangeTime()
        },
        { checked: false, value: 6, label: this.$t('m.all'), rangeTime: [] },
        { checked: false, value: 7, label: this.$t('m.Custom'), rangeTime: [] }
      ],
      params: {
        rangeTime: ''
      },
      total: {
        order: {
          cancelledCount: 0,
          completedAmount: 0,
          completedCount: 0,
          currency: 0,
          exceptionCount: 0,
          exceptionPct: 0,
          lostCount: 0,
          refundedAmount: 0,
          refundedCount: 0,
          rentingCount: 0,
          surveyingCount: 0,
          totalAmount: 0,
          totalCount: 0,
          totalTodayCount: 0,
          unpaidAmount: 0,
          unpaidCount: 0
        },
        user: {
          cardCount: 0,
          depositCount: 0,
          otherCount: 0,
          rentalCount: 0,
          userCount: 0,
          userFreezeCount: 0,
          userNormalCount: 0,
          userToadyCount: 0
        },
        agent: {
          currency: 'sgd',
          revenueAmount: 0,
          totalCount: 0,
          withdrawalAmount: 0
        },
        shop: {
          agentTotalCount: 0,
          currency: 'sgd',
          platformTotalCount: 0,
          revenueAmount: 0,
          totalCount: 0,
          withdrawalAmount: 0
        },
        station: {
          activatedCount: 0,
          activatedLockCount: 0,
          activatedOfflineCount: 0,
          activatedOnlineCount: 0,
          cabinetLock: 0,
          cabinetOffline: 0,
          disable: 0,
          online: 0,
          toBeActivated: 0,
          total: 0,
          withScreenCount: 0,
          withoutScreenCount: 0
        },
        powerBank: {
          abnormalCount: 0,
          broken: 0,
          forceEject: 0,
          lease: 0,
          lostReported: 0,
          soldCount: 0,
          stockCount: 0,
          total: 0,
          unused: 0
        }
      },
      dataAuth: this.permissionsId(93),
      userType: this.getUserType(), //用户类型
      agentLevel: this.getAgentLevel() //代理商等级
    }
  },
  created () {
    if (!this.dataAuth) {
      return
    }
    this.timeDefaultShow = new Date()
    this.timeDefaultShow.setMonth(new Date().getMonth() - 1)
    this.params.rangeTime = this.optionsTab.find(item => item.checked).rangeTime
    this.getTotal()
  },
  methods: {
    getTotal () {
      this.$api.dataStatistics.getRental(this.params).then(res => {
        if (this.$ok(res)) {
          this.total.order = res.data
        }
      })
      if (this.agentLevel != 2) {
        this.$api.dataStatistics.getAgent(this.params).then(res => {
          if (this.$ok(res)) {
            this.total.agent = res.data
          }
        })
      }
      if (this.userType == 1) {
        this.$api.dataStatistics.getUser(this.params).then(res => {
          if (this.$ok(res)) {
            this.total.user = res.data
          }
        })
      }
      this.$api.dataStatistics.getBranch(this.params).then(res => {
        if (this.$ok(res)) {
          this.total.shop = res.data
        }
      })
      this.$api.dashboard.getDeviceStatusOverview().then(res => {
        if (this.$ok(res)) {
          this.total.station = res.data.homeCabinetDTO
          this.total.powerBank = res.data.homePowerBankDTO
        }
      })
    },
    // 切换tab
    switchTab (row) {
      // console.log('switchTab', row.value)
      this.selectTab = row.value
      this.params.rangeTime = row.rangeTime
      this.optionsTab.forEach(item => {
        item.checked = item.value == row.value
      })
      this.getTotal()
    },
    // 改变时间
    changeRangeTime (val) {
      // console.log('changeRangeTime', val)
      this.params.rangeTime = val
      this.getTotal()
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  width: 100%;
  min-width: 1000px !important;
  .page-header {
    padding: 20px 0;
    width: 100%;
    min-width: 1000px !important;
    max-width: 1000px !important;
  }
  .data_div {
    display: flex;
    align-items: center;
    justify-content: start;
    flex-wrap: wrap;
    // margin-bottom: 20px;
  }
  .data_div_div {
    // margin-left: 0px !important;
    // padding: 0 0 20px 0 !important;
    display: flex;
    align-items: center;
    justify-content: start;
    margin-right: 20px;
    margin-bottom: 20px;
    // height: 48px;
  }

  .date_picker {
    ::v-deep .el-input__inner {
      height: 36px !important;
      line-height: 36px !important;
      border-radius: 4px;
      margin-left: 0px !important;
    }
    ::v-deep .el-range-separator {
      padding: 0 !important;
      line-height: 28px !important;
    }
    ::v-deep .el-date-editor .el-range__icon {
      line-height: 28px !important;
    }
    ::v-deep .el-range__close-icon {
      line-height: 28px !important;
    }
  }

  .gray_btn {
    min-width: 120px;
    height: 36px;
    background: rgba(247, 247, 249, 0);
    border: 1px solid #d2d8e2;
    border-radius: 4px;
    cursor: pointer;
    margin-right: 10px;
    font-size: 14px;
    font-weight: 400;
    text-align: center;
    color: #646b80;
    display: flex;
    align-items: center;
    justify-content: center;
    // flex-wrap: wrap;
    // line-height: 36px;
    padding: 12px;
  }
  .active1 {
    border: 1px solid var(--text-color);
    color: var(--text-color);
  }
  .statistics_div {
    column-count: 2;
    grid-gap: 20px;
    .statistics_card {
      width: 490px;
      background: #ffffff;
      border-radius: 10px;
      margin-bottom: 20px;
      padding: 20px;
      break-inside: avoid;
      .statistics_card_top {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-bottom: 20px;
        .statistics_card_title {
          font-size: 16px;
          font-weight: 500;
          text-align: left;
          color: #39404d;
          line-height: 22px;
        }
        .statistics_card_right {
          display: flex;
          align-items: center;
          cursor: pointer;
          span {
            font-size: 14px;
            font-weight: 500;
            text-align: right;
            color: var(--text-color);
            line-height: 16px;
            margin-right: 2px;
          }
          .el-icon-arrow-right {
            font-size: 9px;
            color: var(--text-color);
            font-weight: 700;
          }
        }
      }
      .statistics_card_div {
        width: 100%;
        // border-radius: 4px;
        border: 1px solid #ececee;
        border-bottom: none;
        .statistics_card_div_top {
          display: flex;
          align-items: center;
          justify-content: center;
          background: #f2f5fa;
          border-bottom: 1px solid #ececee;
          .statistics_card_top_title1 {
            flex: 1;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-wrap: wrap;
            padding: 0 20px;
            font-size: 14px;
            font-weight: 500;
            text-align: center;
            color: #39404d;
            line-height: 16px;
          }
          .statistics_card_top_title2 {
            width: 120px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-wrap: wrap;
            padding: 0 8px;
            font-size: 14px;
            font-weight: 500;
            text-align: center;
            color: #39404d;
            line-height: 16px;
          }
        }
        .statistics_card_content {
          background: #ffffff !important;
          border-top: none !important;
          .statistics_card_top_title1 {
            color: #646b80;
          }
          .statistics_card_top_title2 {
            color: #646b80;
          }
        }
      }

      .statistics_card_line1 {
        // border-right: 1px solid #ececee;
        border-left: 1px solid #ececee;
      }
      .statistics_card_line2 {
        border-left: 1px solid #ececee;
      }
    }
  }
}
@media screen and (min-width: 1221px) {
  .container {
    display: flex;
    justify-content: center;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
  }
}
</style>
